﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace NibbleOil.Web.Account
{
    public partial class RecoveryPass : System.Web.UI.Page
    {       
        protected void Page_Load(object sender, EventArgs e)
        {            
        }

        protected void btnEnviar_Click(object sender, EventArgs e)
        {           
            Business.Model.USUARIO oUser = Business.Helper.User.GetData(txtEmail.Text);

            if (oUser != null)
            {                

                string sEmailFrom = string.Empty;
                if ( !string.IsNullOrEmpty(Business.Helper.Parameter.GetValue(Business.Enumeration.ParameterEnum.Name.EmailFrom)) )
                    sEmailFrom = Business.Helper.Parameter.GetValue(Business.Enumeration.ParameterEnum.Name.EmailFrom);

                string sSubject = string.Empty;
                if (!string.IsNullOrEmpty(Business.Helper.Parameter.GetValue(Business.Enumeration.ParameterEnum.Name.RecoveryPassEmailSubject)))
                    sSubject = Business.Helper.Parameter.GetValue(Business.Enumeration.ParameterEnum.Name.RecoveryPassEmailSubject);

                string sBody = string.Empty;
                if (!string.IsNullOrEmpty(Business.Helper.Parameter.GetValue(Business.Enumeration.ParameterEnum.Name.RecoveryPassEmailBody)))
                {
                    sBody = string.Format(Business.Helper.Parameter.GetValue(Business.Enumeration.ParameterEnum.Name.RecoveryPassEmailBody), oUser.Clave);
                }

                bool bResult = Business.Helper.Mail.SendMail(sEmailFrom, oUser.Email, sSubject, sBody);
                txtEmail.Text = string.Empty;

                if (bResult)
                {

                    CustomValidator2.ErrorMessage = Business.Helper.Parameter.GetValue(Business.Enumeration.ParameterEnum.Name.EmailMsgOk);
                    CustomValidator2.IsValid = false;                    
                }
                else
                {
                    CustomValidator1.ErrorMessage = Business.Helper.Parameter.GetValue(Business.Enumeration.ParameterEnum.Name.EmailMsgError);
                    CustomValidator1.IsValid = false; 
                }
            }
            else
            {
                CustomValidator1.ErrorMessage = Business.Helper.Parameter.GetValue(Business.Enumeration.ParameterEnum.Name.RecoveryPassEmailError);
                CustomValidator1.IsValid = false;
            }
        }
    }
}